package controllers

import (
	"are-you-hungry-db/models"
	json2 "encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"time"
)

type OrderController struct {
	beego.Controller
}

func (c *OrderController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	page, _ := c.GetInt("page", 1)
	key := fmt.Sprintf("order:page=%d", page)
	jsonget, _ := models.Rdb.Get(models.Ctx, key).Result()
	var orders []models.Order
	var err error
	if jsonget == "" {
		var FromFindOrder models.Order
		orders, err = FromFindOrder.GetOrderList(page)
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
		jsonset, _ := json2.Marshal(&orders)
		models.Rdb.Set(models.Ctx, key, jsonset, 48*time.Hour)
	} else {
		json2.Unmarshal([]byte(jsonget), &orders)
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "查询成功",
		"Data": orders,
	}
	c.ServeJSON()
	return
}
func (c *OrderController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	var order models.Order
	order.ID = uint(id)

}
